package com.ds.lens.manager.service;

import com.alibaba.fastjson.JSONObject;
import com.ds.lens.data.common.util.HttpUtil;
import com.ds.lens.manager.bo.MuteTaskBO;
import com.ds.lens.manager.scheduler.MuteScheduleConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WeiShaoying
 * @date 2019/11/18 下午3:19
 */
@Service
public class MuteService {

    @Autowired
    private MuteScheduleConfig muteScheduleConfig;

    @Value(value = "${mute.schedule.list.url}")
    private String muteListServiceUrl;

    public void run() {
        Map<Long, MuteTaskBO> muteTaskBOMap = new HashMap<>();
        ResponseEntity responseEntity = HttpUtil.get(muteListServiceUrl, null, null, 5000);
        if (responseEntity.getStatusCode().value() == 200) {
            JSONObject object = JSONObject.parseObject(responseEntity.getBody().toString());
            if (object.getInteger("code") == 0) {
                List<MuteTaskBO> taskBOS = object.getJSONArray("data").toJavaList(MuteTaskBO.class);
                taskBOS.forEach(muteTaskBO -> {
                    muteTaskBOMap.put(muteTaskBO.getId(), muteTaskBO);
                });
            }
        }
        muteScheduleConfig.refreshTasks(muteTaskBOMap);
    }
}
